package uk.ac.lkl.lpp.model.xml;



import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import uk.ac.lkl.common.util.ds.NotifyingList;
import uk.ac.lkl.lpp.model.PedagogyPlanModel;
import uk.ac.lkl.lpp.model.ldo.AssessmentMethod;


public class AssessmentMethodSetProcessor extends ModelElementProcessor {

    public AssessmentMethodSetProcessor() {
        super("AssessmentMethodSet");
    }


    public void convertDataToElements(PedagogyPlanModel plan,
                                      Document document,
                                      Element pedagogyPlanElement) {
        // should be done in base class
        Element parentElement = document.createElement("AssessmentMethodSet");
        pedagogyPlanElement.appendChild(parentElement);

        NotifyingList<AssessmentMethod> list = (NotifyingList<AssessmentMethod>) plan
                .getList(AssessmentMethod.class);
        for (int i = 0; i < list.size(); i++) {
            AssessmentMethod assessmentMethod = list.get(i);
            Element childElement = document.createElement("AssessmentMethod");
            childElement.setAttribute("index", Integer.toString(i));

            String title = assessmentMethod.getTitle();
            childElement.setAttribute("title", title);

            String description = assessmentMethod.getDescription();
            childElement.setTextContent(description);

            parentElement.appendChild(childElement);
        }
    }


    @Override
    public void convertElementToData(PedagogyPlanModel plan,
                                     Element parentElement) {
        NotifyingList<AssessmentMethod> assessmentMethods = new NotifyingList<AssessmentMethod>(
                AssessmentMethod.class);
        NodeList nodes = parentElement.getElementsByTagName("AssessmentMethod");
        for (int i = 0; i < nodes.getLength(); i++) {
            Element childElement = (Element) nodes.item(i);
            String title = childElement.getAttribute("title");
            String description = childElement.getTextContent();
            AssessmentMethod assessmentMethod = new AssessmentMethod(title,
                    description);
            assessmentMethods.add(assessmentMethod);
        }
        plan.addList(AssessmentMethod.class, assessmentMethods);
    }

}
